/*
 * Inspiration: http://www.antonioshome.net/kitchen/swingnbrcp/swingnbrcp-nodes1.php
 */
package org.tintuna.cca.data;

import org.tintuna.cca.dao.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.tintuna.cca.ability.Reloadable;
import org.tintuna.cca.dao.SpreadsheetReader;

/**
 *
 * @author bsmith
 */
public class Spreadsheet implements Lookup.Provider {

    private List<String> headers = null;
    private List<Row> rows = null;
    private Lookup lookup;
    private InstanceContent instanceContent;

    public Spreadsheet() {
        this(new NABSpreadsheetReader2());
    }

    public Spreadsheet(final SpreadsheetReader spreadsheetreader) {
        headers = new ArrayList();
        rows = new ArrayList();
        this.instanceContent = new InstanceContent();
        this.lookup = new AbstractLookup(instanceContent);
        this.instanceContent.add(new Reloadable() {
            @Override
            public void reload() throws Exception {
                //NABSpreadsheetReader2 nssr = new NABSpreadsheetReader2();
                headers.clear();
                rows.clear();
                addSpreadsheetData((SpreadsheetReader) spreadsheetreader);
            }
        });
    }

    public String getName() {
        return "NAB Spreadsheet";
    }
    
    private void addSpreadsheetData(SpreadsheetReader ssreader) {
       List<String> headers = ssreader.getHeaderList();
       setHeaders(headers);

       for (Row r : ssreader.getRows()) {
           addToRows(r);
       }
    }

    public List<String> getHeaders() {
        return headers;
    }

    public void setHeaders(List<String> headers) {
        this.headers = headers;
    }

    public void addToHeader(String headerItem) {
        headers.add(headerItem);
    }

    public List<Row> getRows() {
        return rows;
    }

    public void setRows(List<Row> rows) {
        this.rows = rows;
    }

    public void addToRows(Row row) {
        rows.add(row);
    }

    @Override
    public Lookup getLookup() {
        return lookup;
    }



}
